package cn.uyunsky.platform.controller;

import cn.uyunsky.platform.data.UData;
import cn.uyunsky.platform.data.UDataTool;
import cn.uyunsky.platform.service.ServiceExecute;
import cn.uyunsky.platform.vo.ModelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/logon")
public class LoginController {

    static Logger logger = LoggerFactory.getLogger(LoginController.class);

    private ServiceExecute serviceExecute;

    public void setServiceExecute(ServiceExecute serviceExecute) {
        this.serviceExecute = serviceExecute;
    }

    @RequestMapping(value = "/login")
    public ModelAndView login(
            @RequestParam("userid") String userid,
            @RequestParam("password") String password,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response) {

        logger.debug("登陆请求开始;登陆用户名:" + userid);

        UData uData = UDataTool.createOneUData(new String[]{"userid", "password"}, new Object[]{userid, password});
        UData resdata = serviceExecute.execute("LoginService", uData);
        if (resdata.getHead().isSuccess()) {
            Map resbody = resdata.getBody();
            for (String key : ControllerTool.sessionKey) {
                session.setAttribute(key, resbody.get(key));
            }

            return new ModelAndView(new RedirectView("success.do"));
        } else {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");

            PrintWriter out;
            try {
                out = response.getWriter();
                StringBuilder builder = new StringBuilder();
                builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
                builder.append("alert(\" " + resdata.getHead().getMsg() + " \");");
                builder.append("window.top.location.href=\"");
                builder.append(request.getContextPath());
                builder.append("/index.jsp\";</script>");
                out.print(builder.toString());
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    @RequestMapping(value = "/success")
    public String success(Model model, HttpSession session) {
        String userid = ControllerTool.getUseridBySession(session);

        UData uData = UDataTool.createOneUData(new String[]{"userid"}, new Object[]{userid});
        UData resdata = serviceExecute.execute("MenuService", uData);

        if (resdata.getHead().isSuccess()) {
            List<ModelVO> moduleList = (List<ModelVO>) resdata.getBody().get("moduleList");
            if (logger.isDebugEnabled())
                logger.debug("菜单:" + moduleList);

            model.addAttribute("moduleList", moduleList);
        } else {
            logger.error("获取菜单树失败!!!");
        }
        return "main";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        for (String key : ControllerTool.sessionKey) {
            session.removeAttribute(key);
        }
        return "redirect:/index.jsp";
    }


}
